/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Main;
import model.Vare;
import model.Varegruppe;

/**
 *
 * @author Jonas
 */
public class VareHandler {

    private ArrayList<Varegruppe> vgList;
    private ArrayList<Vare> vareList;
    private ResultSet rs;

    public VareHandler() {
        vgList = new ArrayList<>();
        vareList = new ArrayList<>();

        createList();
    }

    public void createList() {
        String sql = "call getVarer()";
        try {

            rs = Main.dh.getStmt().executeQuery(sql);
            int currentID = 0;

            while (rs.next()) {
                if (currentID != rs.getInt("vg_id")) {
                    Varegruppe vg = new Varegruppe(rs.getInt("vg_id"), rs.getString("vg_navn"));
                    vgList.add(vg);
                }
                if (rs.getInt("vare_id") != 0) {
                    Vare vare = new Vare(rs.getInt("vare_id"), rs.getString("vare_navn"), rs.getInt("vare_pris"), rs.getInt("vare_antal"), rs.getInt("vg_id"));
                    vareList.add(vare);
                }
                currentID = rs.getInt("vg_id");
            }
            System.out.println("Færdig med at lave listerne. Der er: " + vgList.size() + " varegrupper og: " + vareList.size() + " varer.");
        } catch (SQLException ex) {
            Logger.getLogger(VareHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Varegruppe> getVgList() {
        return vgList;
    }

    public ArrayList<Vare> getVareList() {
        return vareList;
    }

}
